#import "DDCouponSeg.h"
CGFloat const JTWCOUPONSEGHEIGHT = 55;
@interface DDCouponSeg()
@property (nonatomic, strong) DDButton *usableBtn;
@property (nonatomic, strong) DDButton *unusableBtn;
@property (nonatomic, strong) UIView *line;
@property (nonatomic, strong) UIView *animLine;
@property (nonatomic, assign) CGSize ksize;
@end
@implementation DDCouponSeg
- (instancetype)initWithSize:(CGSize)size
{
    self = [super init];
    if (self)
    {
        _currentIndex = 100;
        self.ksize = size;
        self.line.hidden = NO;
        self.usableBtn.title = @"可用优惠券";
        self.unusableBtn.title = @"不可用优惠券";
    }
    return self;
}
- (DDButton *)usableBtn
{
    if (!_usableBtn)
    {
        _usableBtn = [[DDButton alloc] init];
        _usableBtn.tag = 200;
        _usableBtn.font = FONT(UIFontWeightRegular, 17);
        _usableBtn.autoTitleSize = YES;
        [_usableBtn setValue:TEXT_GRAY_COLOR forKeyPath:@"_label.textColor"];
        [_usableBtn addTarget:self action:@selector(dd_actionForUsableBtn:) forControlEvents:UIControlEventTouchUpInside];
        [self addSubview:_usableBtn];
        _usableBtn.c_centerX.c_equal(self).with.coffset(- self.ksize.width / 4.0);
        _usableBtn.c_centerY.c_equal(self);
    }
    return _usableBtn;
}
- (DDButton *)unusableBtn
{
    if (!_unusableBtn)
    {
        _unusableBtn = [[DDButton alloc] init];
        _unusableBtn.tag = 201;
        _unusableBtn.font = FONT(UIFontWeightRegular, 17);
        _unusableBtn.autoTitleSize = YES;
        [_unusableBtn setValue:TEXT_GRAY_COLOR forKeyPath:@"_label.textColor"];
        [_unusableBtn addTarget:self action:@selector(dd_actionForUsableBtn:) forControlEvents:UIControlEventTouchUpInside];
        [self addSubview:_unusableBtn];
        _unusableBtn.c_centerX.c_equal(self).with.coffset( self.ksize.width / 4.0);
        _unusableBtn.c_centerY.c_equal(self);
    }
    return _unusableBtn;
}
- (UIView *)line
{
    if (!_line)
    {
        _line = [[UIView alloc] init];
        _line.backgroundColor = CELL_LINE_COLOR;
        [self addSubview:_line];
        _line.c_left.c_equal(self);
        _line.c_right.c_equal(self);
        _line.c_bottom.c_equal(self);
        _line.c_height.c_equal(1.0);
    }
    return _line;
}
- (UIView *)animLine
{
    if (!_animLine)
    {
        _animLine = [[UIView alloc] init];
        _animLine.backgroundColor = [DDHelper help].color;
        [self addSubview:_animLine];
        _animLine.c_bottom.c_equal(self);
        _animLine.c_height.c_equal(1.0);
    }
    return _animLine;
}
- (void)setCurrentIndex:(int)currentIndex
{
    if (_currentIndex != currentIndex)
    {
        _currentIndex = currentIndex;
        [self.usableBtn setValue:(_currentIndex == 0)?[DDHelper help].color:TEXT_GRAY_COLOR forKeyPath:@"_label.textColor"];
        [self.unusableBtn setValue:(_currentIndex == 0)?TEXT_GRAY_COLOR:[DDHelper help].color forKeyPath:@"_label.textColor"];
        if (!_animLine)
        {
            [self dd_updateAnimLineCenterX];
        }
        else
        {
            [UIView animateWithDuration:0.3 animations:^{
                if (_currentIndex == 0)
                    self.animLine.transform = CGAffineTransformTranslate(self.animLine.transform, - (self.unusableBtn.frame.origin.x - self.usableBtn.frame.origin.x)-GAP, 0);
                else
                    self.animLine.transform = CGAffineTransformTranslate(self.animLine.transform, (self.unusableBtn.frame.origin.x - self.usableBtn.frame.origin.x)+GAP, 0);
            } completion:^(BOOL finished) {
                if (self.block)
                {
                    self.block();
                }
            }];
        }
        [self dd_updateAnimLineWidth];
    }
}
- (void)dd_segRoll
{
    self.usableBtn.c_centerX.c_equal(self).with.coffset(-self.ksize.width / 4.0);
    self.unusableBtn.c_centerX.c_equal(self).with.coffset(self.ksize.width / 4.0);
    self.animLine.transform = CGAffineTransformIdentity;
    [self dd_updateAnimLineCenterX];
    [self dd_updateAnimLineWidth];
}
- (void)dd_updateAnimLineCenterX
{
    CGFloat centerX = (_currentIndex == 0)?(-self.ksize.width / 4.0):(self.ksize.width / 4.0);
    self.animLine.c_centerX.c_equal(self).with.coffset(centerX);
}
- (void)dd_updateAnimLineWidth
{
    CGFloat width = (_currentIndex == 0)?self.usableBtn.bounds.size.width:self.unusableBtn.bounds.size.width;
    self.animLine.c_width.c_equal(width);
}
- (void)dd_actionForUsableBtn:(DDButton *)sender
{
    self.currentIndex = (int)sender.tag - 200;
}
@end
